package com.google.code.soumi.web;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.jsplite.mvc.Action;
import com.google.code.jsplite.mvc.ModelView;
import com.google.code.soumi.dao.Users;
import com.google.code.soumi.service.SoumiService;

public class IndexAction implements Action {
	
	private SoumiService soumiService = SoumiService.getInstance();

	@Override
	public ModelView execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelView modelView = new ModelView();		
		NumberFormat nf;
		

		//已查过的人数
		long queryCount = soumiService.getQueryCount();
		nf = new DecimalFormat("000000000");
		char[] num = nf.format(queryCount).toCharArray();
		modelView.addObject("num", num);
		
		
		//命中率
		long hitCount = soumiService.getHitCount();
		nf = new DecimalFormat("###.#");
		String hitRate = nf.format((double)hitCount/(double)queryCount*100);
		modelView.addObject("hitRate", hitRate);		
		
		
		//查询关键字
		String key = request.getParameter("key")==null?"":request.getParameter("key");
		modelView.addObject("key", key);
		
		//头像表情
		if(key.equals("")){
			modelView.addObject("emotion", "Normal");
			return modelView;
		}
		
		
		//查询数据
		List<Users> list = soumiService.getUserList(key);
		modelView.addObject("list", list);		
		
		//头像表情
		if(list==null || list.size()<=0){
			modelView.addObject("emotion", "Good");
		}else if(list.size()<=5){
			modelView.addObject("emotion", "Bad");
		}else{
			modelView.addObject("emotion", "Hell");
		}
		
		
		return modelView;	
	}


}
